﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;

namespace MyNanny
{
    public class DomainsList : List<Domain>
    {
        private FileInfo fileName = null;

        public DomainsList() : base()
        {
            GetFileName();
            Load();
        }

        public virtual FileInfo HostsFile
        {
            get { return fileName; }
        }
        
        private FileInfo GetFileName()
        {
            string tmpResult = (Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "DataBasePath", @"%SystemRoot%\System32\drivers\etc")).ToString();

            if (!string.IsNullOrEmpty(tmpResult))
                tmpResult = "%SystemRoot%\\System32\\drivers\\etc\\hosts";

            tmpResult = Environment.ExpandEnvironmentVariables(tmpResult);

            fileName = new FileInfo(tmpResult);
            return fileName;
        }

        public virtual Domain Add(bool Enabled, string IP, string Host, string Comment)
        {
            Domain domain = new Domain(Enabled, IP, Host, Comment);
            Add(domain);
            return domain;
        }

        public virtual bool Load()
        {
             bool result = false;
             if (HostsFile.Exists)
             {
                 FileStream hosts = new FileStream(HostsFile.FullName, FileMode.Open, FileAccess.Read);
                 StreamReader reader = new StreamReader(hosts);

                 string file = reader.ReadToEnd();
                 reader.Close();

                 Clear();

                 MatchCollection matchs = Regex.Matches(file, Domain.SearchExpression, RegexOptions.Compiled | RegexOptions.Multiline);
                 foreach (Match match in matchs) {
                     if (match.Groups[0].Value.Trim().Length > 0) {
                         if (match.Groups[Domain.Search_TheHostRecord].Value.Trim().Length == 0)
                         //Just a comment line
                         {
                             Add(new Domain(match.Groups[Domain.Search_Comment].Value.Trim()));
                         }
                         else {
                             string comment = match.Groups[Domain.Search_Comment].Value.Trim();
                             Add(new Domain(
                                 match.Groups[Domain.Search_EnabledFlag].Length == 0,
                                 match.Groups[Domain.Search_IPAddress].Value.Trim(),
                                 match.Groups[Domain.Search_HostName].Value.Trim(),
                                 (comment.Length > 1) ? comment.Substring(1) : comment));
                         
                         }
                     
                     
                     
                     }
                 }
                 result = true;
             }
             return result;
        }

        public virtual bool Save() {
            throw new NotImplementedException();
        }
    }
}
